package exemplos.ex02.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import exemplos.ex02.modelo.FachadaUsuario;

/**
 * Servlet implementation class ProcessaLogin1
 */
@WebServlet("/login")
public class ProcessaLogin1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProcessaLogin1() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String login = request.getParameter("login");
		if (login != null && !login.equals("")) {
			FachadaUsuario fachada = new FachadaUsuario();
			if (fachada.existeUsuario(login)) {
				HttpSession session = request.getSession(true);
				session.setMaxInactiveInterval(10);
				session.setAttribute("login", login);
				request.getRequestDispatcher("exemplo02/informaSenha.html").forward(request, response);
				return;
			}
		}
		response.sendRedirect("exemplo02/erroLogin.html");
	}

}
